home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
BSKBD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-06-07
|
6KB
|
282 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: bskbd.c
// Title: Base library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains code to read a keyboard. It is fairly specific to DOS,
// but it will work (with some restrictions) under UNIX.
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <bs.h>
//----------------------------------------------------------------------------
// Globals
//----------------------------------------------------------------------------
#if OS_DOS
static SHORT sRead = 0x00;
static SHORT sReady = 0x01;
static SHORT sState = 0x02;
static BOOL fExtended = FALSE;
#endif
#if OS_UNIX
# ifndef getch
# define getch() getc(stdin)
# endif
#endif
//----------------------------------------------------------------------------
// Description: Read a character from the keyboard.
// Parameters:
// Returns: Character read from keyboard
//----------------------------------------------------------------------------
INT FN_EA KbdChar(void)
{
#if OS_DOS
SHORT sKey;
SHORT _sRead = sRead;
_asm {
mov ah, BYTE PTR [_sRead]
int 16h
mov [sKey], ax
}
if ((sKey & 0x00FF) == 0x00E0)
sKey &= 0xFF00;
else if (sKey & 0x00FF)
sKey &= 0x00FF;
return sKey;
#elif OS_PM || OS_WINDOWS
return 0;
#elif OS_OS2
INT ch = (INT)getch();
if (ch == 0)
{
ch = (INT)getch();
ch <<= 8;
}
return ch;
#else
INT ch = (INT)getchar();
if (ch == 0)
{
ch = (INT)getchar();
ch <<= 8;
}
return ch;
#endif
}
//----------------------------------------------------------------------------
// Description: Flush the keyboard.
// Parameters:
// Returns: void
//----------------------------------------------------------------------------
VOID FN_E KbdFlush(void)
{
while (KbdReady())
KbdChar();
return ;
}
//----------------------------------------------------------------------------
// Description: Initialize the keyboard interface.
// Parameters: void
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
VOID FN KbdInitialize(void)
{
#if OS_DOS
fExtended = ((*(PUSHORT)(0x00400096L)) & 0x10);
sRead = (fExtended) ? 0x10: 0x00;
sReady = (fExtended) ? 0x11: 0x01;
sState = (fExtended) ? 0x12: 0x02;
return ;
#endif
}
//----------------------------------------------------------------------------
// Description: Check keyboard insert state
// Parameters:
// Returns: TRUE if insert mode is on
//----------------------------------------------------------------------------
BOOL FN_E KbdIsInsert(void)
{
#if OS_UNIX || OS_PM || OS_WINDOWS
return FALSE;
#else
return (KbdState()) != 0; // added visual c++
// return (KbdState() & INSERT_ON) != 0;
#endif
}
//----------------------------------------------------------------------------
// Description: Check if a keystroke is waiting to be read.
// Parameters: void
// Returns: Keystroke waiting to be read or 0.
//----------------------------------------------------------------------------
INT FN_EA KbdReady(void)
{
#if OS_DOS
SHORT sKey;
SHORT _sReady = sReady;
_asm {
mov ah, BYTE PTR [_sReady]
int 16h
jnz Ready1
xor ax, ax
mov [sKey], ax
jmp Ready2
}
Ready1:
_asm {
mov [sKey], ax
}
Ready2:
if ((sKey & 0x00FF) == 0x00E0)
sKey &= 0xFF00;
else if (sKey & 0x00FF)
sKey &= 0x00FF;
return sKey;
#elif OS_UNIX
return 0;
#elif OS_PM || OS_WINDOWS
return 0;
#else
return (INT)kbhit();
#endif
}
//----------------------------------------------------------------------------
// Description: Get keyboard state.
// Parameters: void
// Returns: Keyboard state
//----------------------------------------------------------------------------
USHORT FN_EA KbdState(void)
{
#if OS_DOS
USHORT usState;
SHORT _sState = sState;
_asm {
mov ah, BYTE PTR [_sState]
int 16h
mov [usState], ax
}
if (!fExtended)
usState &= 0x00FF;
return usState;
#else
return 0;
#endif
}
//----------------------------------------------------------------------------
// Description: Run standard test suite
// Parameters: sTest Test to run.
// 0 Run all default tests (except).
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
#if COMPILE_TEST
BOOL FN KbdTest(SHORT sTest)
{
SHORT sKey;
USHORT usState;
NOTUSED(sTest);
KbdFlush();
Output("Insert mode is %d.\n", KbdIsInsert());
while (!(sKey = KbdReady()))
;
sKey = KbdChar();
Output("Character = %c (%04X)\n", sKey, sKey);
#if OS_UNIX == 0
Output("Press LEFT shift to continue:\n");
do
{
usState = KbdState();
}
while (!(usState & LEFTSHIFT));
#endif
return TRUE;
}
#endif
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------